import axios from 'axios';
import Cbct from './base.js';
import md5 from 'js-md5';
axios.defaults.baseURL = 'http://adm.test.cbct.cn';
export default {
    post: (url,data = {}) => {
        if(Cbct.getCookie('token2') == ''){
            Cbct.setCookie('token2','TOKEN');              
        }
        if(Cbct.getCookie('salt2') == ''){
            Cbct.setCookie('salt2','SALT');
        }
        var tsmTime = Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'));
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId2'),
            'X-cbct-utk':Cbct.getCookie('token2'),
            'X-cbct-tsm':tsmTime,
            'X-cbct-role':Cbct.getCookie('role'),
            'X-cbct-sgn':md5(tsmTime+md5(Cbct.getCookie('token2')+Cbct.H5key)+Cbct.getCookie('salt2')),
            'Content-Type':'application/x-www-form-urlencoded',
        };
        return new Promise((resolve, reject) => {
            axios.post(url, data)
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    },
    get: (url,params = {}) => {
        if(Cbct.getCookie('token2') == ''){
            Cbct.setCookie('token2','TOKEN');              
        }
        if(Cbct.getCookie('salt2') == ''){
          Cbct.setCookie('salt2','SALT');
        }
        var tsmTime = Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'));
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId2'),
            'X-cbct-utk':Cbct.getCookie('token2'),
            'X-cbct-tsm':tsmTime,
            'X-cbct-role':Cbct.getCookie('role'),
            'X-cbct-sgn':md5(tsmTime+md5(Cbct.getCookie('token2')+Cbct.H5key)+Cbct.getCookie('salt2')),
            'Content-Type':'application/x-www-form-urlencoded',
        };
        return new Promise((resolve, reject) => {
            axios.get(url, {params})
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    },
    put: (url,params = {}) => {
        if(Cbct.getCookie('token2') == ''){
            Cbct.setCookie('token2','TOKEN');              
        }
        if(Cbct.getCookie('salt2') == ''){
            Cbct.setCookie('salt2','SALT');
        }
        axios.defaults.headers = {
            'X-cbct-domain': Cbct.getCookie('openId2'),
            'X-cbct-utk':Cbct.getCookie('token2'),
            'X-cbct-tsm':Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing')),
            'X-cbct-role':Cbct.getCookie('role'),
            'X-cbct-sgn':md5(Number(Date.parse(new Date()))+Number(Cbct.getCookie('timing'))+md5(Cbct.getCookie('token2')+Cbct.H5key)+Cbct.getCookie('salt2')),
            'Content-Type':'application/x-www-form-urlencoded',
        };
        return new Promise((resolve, reject) => {
            axios.put(url, {params})
            .then (response => {
                resolve(response.data);
            })
            .catch(error => {
                reject(error);
            })
        })
    }
}
